<?php

namespace Wap\Controller;

//门店

class StoreController extends WapController {

    //门店列表
    public function index() {
        $_SESSION['path'] = 1;
        require_once __DIR__ . "/jssdk.php";
        $jssdk = new JSSDK("wx331ed6d859b117cb", "dbada5df9c35c886582d7a0d05fe3a2c");
        $wxConfig = $jssdk->GetSignPackage();
        $this->assign('wxConfig', $wxConfig);
        $this->display('index_pre');
    }
    //门店的处理
    public function index_do()
    {

        if(!isset($_GET['bid']))
        {
            if(!(isset($_SESSION['path']) && ($_SESSION['path'] == 1)))
            {
                header('location:'.U('Index/index'));die;
            }
                $_SESSION['path'] = NULL;
                unset($_SESSION['path']);
        }




        //查询出品牌
        $brand = D("Brand")->field("id,bname")->select();
        $this->assign("brand", $brand);
        //根据品牌查询门店查询
        $bid = empty($_GET["bid"]) ? $brand[0]["id"] : $_GET["bid"];

        $where = " where bid=" . $bid;

        $keyword = I("keyword");
        if(!empty($keyword)){
            $where.=" and (sname like '%".$keyword."%')";
        }
        

        /*
        $lon = 106.472825;
        $lat = 29.564383;
        */
        if(isset($_SESSION['lon']) && (($_SESSION['lon_lat_time']+120) > time()))
        {
            $lon = $_SESSION['lon'];
            $lat = $_SESSION['lat'];
        }else{
            $lon = I('lon');
            $lat = I('lat');
            if(empty($lon) && isset($_SESSION['lon']))
            {
                $lon = $_SESSION['lon'];
                $lat = $_SESSION['lat'];
            }

            $lon = is_numeric($lon) ? $lon : 106.472825;
            $lat = is_numeric($lat) ? $lat : 29.564383;
            
            if(strlen(strval($lon)) <= 11 )
            {
                $json = file_get_contents("http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x={$lon}&y={$lat}");
                $xyArr = json_decode($json,true);
                $lon = isset($xyArr['x']) ? base64_decode($xyArr['x']) : $lon;
                $lat = isset($xyArr['y']) ? base64_decode($xyArr['y']) : $lat;
                $_SESSION['lon'] = $lon;
                $_SESSION['lat'] = $lat;
                $_SESSION['lon_lat_time'] = time();
            }
        }


        //当前页数
        $indexpage = empty($_GET["indexpage"])?1:$_GET["indexpage"];
             //  var_dump($indexpage);
        $pagesize = 5;
        $sql = 'select pic,id,sname,(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*(' . $lat . '-lat)/360),2)+COS(PI()*33.07078170776367/180)* COS(lat * PI()/180)*POW(SIN(PI()*(' . $lon . '-lon)/360),2)))) as store from `faxiu_store` ' . $where . ' order by store asc limit '.($indexpage-1)*$pagesize.','.$pagesize;
        
        $sql2 = 'select pic,id,sname,ROUND(6378.138 * 2 * 
                                ASIN(
                                        SQRT(
                                                POW(
                                                    SIN(('.$lat.'*PI()/180-lat*PI()/180)/2),
                                                        2
                                                        )
                                                  +COS('.$lat.'*PI()/180)
                                                  *COS(lat*PI()/180)
                                                  *POW(
                                                        SIN(('.$lon.'*PI()/180-lon*PI()/180)/2),
                                                             2
                                                      )

                                            )
                                    )*1000
                             ) as store from `faxiu_store` ' . $where . ' order by store asc limit '.($indexpage-1)*$pagesize.','.$pagesize;


        $sql3 = 'select pic,id,sname,ROUND(6378.138 * 2 * 
                                ASIN(
                                        SQRT(
                                                POW(
                                                    SIN((lat*PI()/180 - '.$lat.'*PI()/180)/2),
                                                        2
                                                        )
                                                  +COS(lat*PI()/180)
                                                  *COS('.$lat.'*PI()/180)
                                                  *POW(
                                                        SIN((lon*PI()/180-'.$lon.'*PI()/180)/2),
                                                             2
                                                      )

                                            )
                                    )*1000
                             ) as store from `faxiu_store` ' . $where . ' order by store asc limit '.($indexpage-1)*$pagesize.','.$pagesize;

        $store = M()->query($sql3);
      // echo M()->getLastSql();
        foreach ($store as $key => $value) {
            $store[$key]["store"] = round($value["store"],2);
            $pic =  explode(",", $store[$key]["pic"]);
            $store[$key]["pic"] =  empty($pic[0])?$pic[1]:$pic[0];
        }
                
//        for($i=0;$i<count($store);$i++){
//            $store[$i]["store"] = round($store[$i]["store"],2);
//        }
//var_dump($store);//die();
        $this->assign("bid", $bid);
        $this->assign("store", $store);
        if(IS_AJAX){
            exit($this->fetch("storelist"));
        }
        $this->display('index');
    } 

    //根据id查询详情
    public function detail(){
        $id = I("id");
        $where["id"] = array("eq", $id);
        $info = D("Store")->where($where)->find();
        $info["store_name"] = $info["lon"].",".$info["lat"];
        $info['store_location'] = implode(',', array_reverse(explode(',',$info["store_name"])));// 经纬度互换位置
        $info['store_name'] = $info['sname']; // 分店名称
        $info['store_address'] = $info['address']; // 分店地址
        $info["hot"]=M('collect')->where(array('tid' => $info['id'], 'type' => 1))->count();
        if($info["pic"]!=null){
            $info["pic"] = explode(",",$info["pic"]);
        }
        $this->assign("info", $info);
        //判断是否已经收藏了 1已收藏 2 未收藏
        $iscollect=$this->_is_collected($id, 1);

        $this->assign("iscollect", $iscollect);
       // var_dump($iscollect);die();
        //查询本店的设计师
        $designer = M("designer")->where(array("sid" => array("eq", $info['id'])))->select();
        $this->assign("designer", $designer);
        $this->display();
    }

    public function driver() {
        require_once __DIR__ . "/jssdk.php";
        $jssdk = new JSSDK("wx331ed6d859b117cb", "dbada5df9c35c886582d7a0d05fe3a2c");
        $wxConfig = $jssdk->GetSignPackage();

        //查询出品牌
        $brand = D("Brand")->field("id,bname")->select();
        $this->assign("brand", $brand);
        $this->assign('wxConfig', $wxConfig);
        $this->display();
    }

}